from threading import Thread
from MsgProcess import MsgProcess, MsgType
import time
import RPi.GPIO as GPIO
pin =16
GPIO.setmode(GPIO.BOARD)      #设置引脚编号规则
GPIO.setup(pin, GPIO.IN)      #将引脚设置成输入模式

class Human_body_detection(MsgProcess):

    def Start(self, message):
        time.sleep(10)
        self.result=""
        self.mythread=Thread( target=self.Human_body_detection_data )
        self.mythread.start()
        

    def Human_body_detection_data(self):

        while 1:
            data = GPIO.input(pin)   #获取高低电平
            if data ==1:
                self.result="有人活动"
            if data ==0:
                self.result="没人活动"
            
            data = {'action':"Human_body_detection","Human_body_detection_Data":self.result, "Human_body_detection_Time":time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) }                 
            self.send(MsgType.Text, Receiver='MqttProxy', Data=data)

            time.sleep(1)

    #执行入口
    def Text(self, message):
        Data = message['Data']
        #微信进入事件
        if type(Data) is dict  and 'initstate' in dict(Data).keys():
            if Data['initstate'] == 'onLoad':
                data = {'action':"Human_body_detection","Human_body_detection_Data":self.result, "Human_body_detection_Time":time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) }                 
                self.send(MsgType.Text, Receiver='MqttProxy', Data=data)
            return 

        self.say(self.result)